package com.fourth_groups.shopping121store.ui.category.adapter;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.fourth_groups.shopping121store.R;
import com.fourth_groups.shopping121store.bean.CategoryGroupLeftLvBean;

import java.util.List;

/**
 * 类名：CategoryLeftLvAdapter
 * 创建人：ZhangPing
 * 邮箱：zp045217@163.com
 * 创建时间：2016/10/12
 * 备注：
 */

public class CategoryLeftLvAdapter extends BaseAdapter{
    private Context context;
    private List<CategoryGroupLeftLvBean.DataBean.CategoryBean> results;
    private ViewHolder holder = null;
    private int selectedId;

    public CategoryLeftLvAdapter(Context context, List<CategoryGroupLeftLvBean.DataBean.CategoryBean> results) {
        this.context = context;
        this.results = results;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return results.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return results.get(position);
    }


    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    public void setSelected(int position)
    {
        this.selectedId = position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        CategoryGroupLeftLvBean.DataBean.CategoryBean c= (CategoryGroupLeftLvBean.DataBean.CategoryBean)getItem(position);

        if(convertView == null)
        {
            holder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.list_item_category_groups, null);
            holder.tv = (TextView) convertView.findViewById(R.id.category_show_group_tv);
        }
        else
        {
            holder = (ViewHolder) convertView.getTag();
        }

        if(position == selectedId)
        {
            //设置左侧导航选中时的背景图片
            convertView.setBackgroundResource(R.mipmap.list_item_all_left_selected1);
        }
        else
        {
            //设置左侧导航未选中时的背景
            convertView.setBackgroundColor(Color.parseColor("#ffffff"));
        }

        holder.tv.setText(c.getName());
        convertView.setTag(holder);

        return convertView;
    }

    class ViewHolder
    {
        TextView tv;
    }
}
